<!DOCTYPE html>
<!--
Copryight 2012 Software Freedom Conservancy
Copyright 2010-2012 WebDriver committers

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>

<head>
  <title>text_test.html</title>
  <meta charset="UTF-8">
  <script src="test_bootstrap.js"></script>
  <script type="text/javascript">
    goog.require('bot.dom');
    goog.require('bot.locators');
    goog.require('goog.testing.jsunit');
    goog.require('goog.testing.ExpectedFailures');
  </script>

  <script type="text/javascript">
    var findElement = bot.locators.findElement;
    var getText = bot.dom.getVisibleText;

    customElements.define('open-shadow-element',
      class extends HTMLElement {
        constructor() {
          super();
          this.attachShadow({ mode: 'open' });
        }
      }
    );

    customElements.define('closed-shadow-element',
      class extends HTMLElement {
        constructor() {
          super();
          this._shadowRoot = this.attachShadow({ mode: 'closed' });
        }
      }
    );

    function setCustomElement(html, type='open') {
      if (type == 'closed') {
        document.querySelector(`closed-shadow-element`)._shadowRoot.innerHTML = html;
      } else {
        document.querySelector(`open-shadow-element`).shadowRoot.innerHTML = html;
      }
    }

    function tearDown() {
      let open = document.querySelector('open-shadow-element');
      let closed = document.querySelector('closed-shadow-element');

      open.shadowRoot.innerHTML = '';
      closed._shadowRoot.innerHTML = '';
    }

    function testTextInShadowDOM() {
      setCustomElement('<div><a href=# id=linkText>full link text</a></div>');

      let customEl = findElement({ tagName: 'open-shadow-element' });
      let text = getText(customEl);
      assertEquals('full link text', text);
    }

    function testTextForSlotValueInShadowDOM() {
      setCustomElement('<slot><span>full</span></slot>');

      let customEl = findElement({ id: 'custom-text' });
      let text = getText(customEl);
      assertEquals('custom', text);
    }

    function testTextForSlotAndExtraValueInShadowDOM() {
      setCustomElement('<slot><span>full</span></slot> text');

      let customEl = findElement({ id: 'custom-text' });
      let text = getText(customEl);
      assertEquals('custom text', text);
    }

    function testTextForDefaultSlotValueInShadowDOM() {
      setCustomElement('<slot><span>full</span></slot> text');

      let customEl = findElement({ tagName: 'open-shadow-element' });
      let text = getText(customEl);
      assertEquals('full text', text);
    }

    function testHiddenTextInShadowDOM() {
      setCustomElement(
        '<div><a href=# id=linkText>full <div style="display:none">link</div> text</a></div>'
      );

      let customEl = findElement({ tagName: 'open-shadow-element' });
      let text = getText(customEl);
      assertEquals('full text', text);
    }

    function testTextForElementWithinClosedShadowDOM() {
      setCustomElement('<a href=# id=linkText>full link text</a>', 'closed');
      let customEl = findElement({ tagName: 'closed-shadow-element' });
      let innerEl = findElement({ css: 'a' }, customEl._shadowRoot);
      let text = getText(innerEl);
      assertEquals('full link text', text);
    }
  </script>
</head>

<body>
  <open-shadow-element></open-shadow-element>
  <open-shadow-element id="custom-text">custom</open-shadow-element>
  <closed-shadow-element></closed-shadow-element>
</body>

</html>
